package com.example.administrator.printoutindemo;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;

import com.example.administrator.printoutindemo.http.SPUtil;
import com.jess.arms.base.BaseConstants;

import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.retrofiturlmanager.RetrofitUrlManager;

import static com.jess.arms.base.BaseConstants.HTTP_URL;

/**
 * Created by Administrator on 2018/9/9.
 */

public class SetActivity extends AppCompatActivity {
    SharedPreferences users;
    private EditText txtIp, txtPort;
    private Button btnSave, btnQuit;
    private CheckBox cb_jzmms;
    private RadioButton rb_er_wei_ma_20, rb_er_wei_ma_50;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        ToolbarUtils.initToolbarTitleBack(this, "设置");

        txtIp = (EditText) findViewById(R.id.txtIp);
        txtPort = (EditText) findViewById(R.id.txtPort);
        btnSave = (Button) findViewById(R.id.btnSave);
        btnQuit = (Button) findViewById(R.id.btnQuit);
        cb_jzmms = (CheckBox) findViewById(R.id.cb_jzmm);
        rb_er_wei_ma_20 = (RadioButton) findViewById(R.id.rb_er_wei_ma_20);
        rb_er_wei_ma_50 = (RadioButton) findViewById(R.id.rb_er_wei_ma_50);
        users = getSharedPreferences("TEXT", Context.MODE_PRIVATE);
        boolean isRemember = users.getBoolean("users", false);
        if (isRemember) {
            String ip = users.getString(BaseConstants.IP, "");
            String port = users.getString(BaseConstants.PORT, "");
            txtIp.setText(ip);
            txtPort.setText(port);
            cb_jzmms.setChecked(true);
        }
        if (users.getString(BaseConstants.ER_WEI_MA_TYPE, "").equals("1")) {
            rb_er_wei_ma_20.setChecked(true);
            rb_er_wei_ma_50.setChecked(false);
        } else {
            rb_er_wei_ma_20.setChecked(false);
            rb_er_wei_ma_50.setChecked(true);
        }
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SPUtil.putString(SetActivity.this, "httpurl", "http://" + txtIp.getText().toString() + ":" + txtPort.getText().toString());
                Log.e("dayin", "http://" + txtIp.getText().toString() + ":" + txtPort.getText().toString());
                SPUtil.putString(SetActivity.this, "duankouhao", txtPort.getText().toString());
                SharedPreferences.Editor editor = users.edit();
                if (cb_jzmms.isChecked()) {
                    editor.putBoolean("users", true);
                    editor.putString("ip", txtIp.getText().toString().replace(" ", "").trim());
                    editor.putString("port", txtPort.getText().toString().replace(" ", "").trim());
                } else {
                    editor.clear();
                }

                if (rb_er_wei_ma_20.isChecked()) {
                    //1 是50x20
                    editor.putString(BaseConstants.ER_WEI_MA_TYPE, "1");
                } else {
                    //2 是50x50
                    editor.putString(BaseConstants.ER_WEI_MA_TYPE, "2");
                }
                editor.commit();

                String httpUrl = SPUtil.getString(SetActivity.this, HTTP_URL, "");
                httpUrl = httpUrl.replace(" ", "");
                RetrofitUrlManager.getInstance().setGlobalDomain(httpUrl);

                finish();
//                Intent intent = new Intent(SetActivity.this, LoginActivity.class);
//                startActivity(intent);
            }
        });
        btnQuit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SetActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
    }
}
